package com.google.code;

import com.google.gdata.util.*;

import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.util.*;

import com.google.gdata.client.projecthosting.IssuesQuery;
import com.google.gdata.client.projecthosting.ProjectHostingService;
import com.google.gdata.data.projecthosting.IssuesEntry;
import com.google.gdata.data.projecthosting.IssuesFeed;

import org.restlet.Restlet;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.DomRepresentation;

import org.w3c.dom.Document;


public class ComponentTags extends Restlet {
	
	public ComponentTags() {
		// TODO Auto-generated constructor stub	
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	}
	
	/**
	 * Permette di comporre il documento xml basandosi sui dati passati in input
	 * 
	 * @param project
	 * @param namecomponent
	 * @param occ
	 * @return
	 */
    public Document composeResponse(String project, String namecomponent, HashMap<String, Integer> occ) {
		
		CountTags test = new CountTags();
		Document doc = test.creaDoc("ComponentTags", project, namecomponent, occ);
		return doc;
	}

	@Override
	public void handle (Request request, Response response)  {
		ArrayList<String> tag = new ArrayList<String>();
		HashMap<String, Integer> occ = new HashMap<String, Integer>();
		ProjectHostingService myService = new ProjectHostingService("Michele-IssueRetrivers-1");
		/*try{
		myService.setUserCredentials("gogetassj24@hotmail.it", "gokussj4");
	    }catch (AuthenticationException e){
	    	e.printStackTrace();
	    }*/
		String namecomponent = request.getAttributes().get("namecomponent").toString();
		String nameproject = request.getAttributes().get("nameproject").toString();
		
		try{
			//decodifica il namecomponent e il nameproject
		    namecomponent= URLDecoder.decode(namecomponent, "ISO-8859-1");
		    nameproject= URLDecoder.decode(nameproject, "ISO-8859-1");	
		    
		    URL feedUrl = new URL("http://code.google.com/feeds/issues/p/" + nameproject + "/issues/full");
			IssuesQuery myQuery = new IssuesQuery(feedUrl);
			myQuery.setLabel("Component-"+namecomponent);
			IssuesFeed resultFeed = myService.query(myQuery, IssuesFeed.class);
			
            //Calcola le occorrenze dei tag non contenti il simbolo che delimita le coppie valore
			for (int i = 0; i < resultFeed.getEntries().size(); i++) {
				  IssuesEntry entry = resultFeed.getEntries().get(i);
				  for(int j = 0; j < resultFeed.getEntries().get(i).getLabels().size(); j++){
				   if((entry.getLabels().get(j).getValue().indexOf("-")!=-1)
				    		 /*(entry.getLabels().get(j).getValue().startsWith("Priority-"))||
					    	(entry.getLabels().get(j).getValue().startsWith("Type-"))||
					    	(entry.getLabels().get(j).getValue().startsWith("OpSys-"))||
					   		(entry.getLabels().get(j).getValue().startsWith("Milestone-"))||
					   		(entry.getLabels().get(j).getValue().startsWith("Component-"))||
					   		(entry.getLabels().get(j).getValue().startsWith("Project-"))||
					   		(entry.getLabels().get(j).getValue().startsWith("Status-"))||
				    		(entry.getLabels().get(j).getValue().startsWith("Summary-"))||
				    		(entry.getLabels().get(j).getValue().startsWith("Comment-"))||
				    		(entry.getLabels().get(j).getValue().startsWith("Opened-"))||
					   		(entry.getLabels().get(j).getValue().startsWith("Modified-"))||
					   		(entry.getLabels().get(j).getValue().startsWith("Closed-"))||
					   		(entry.getLabels().get(j).getValue().startsWith("Owner-"))||
				    		(entry.getLabels().get(j).getValue().startsWith("Cc-"))||
				    		(entry.getLabels().get(j).getValue().startsWith("Reporter-"))||
				    		(entry.getLabels().get(j).getValue().startsWith("CommentBy-"))||
				    		(entry.getLabels().get(j).getValue().startsWith("Stars-"))||
				    		(entry.getLabels().get(j).getValue().startsWith("Blocked-"))||
				    		(entry.getLabels().get(j).getValue().startsWith("Blocking-"))||
				    		(entry.getLabels().get(j).getValue().startsWith("BlockedOn-"))||
				    		(entry.getLabels().get(j).getValue().startsWith("Category-"))*/);
				   else{	       
				    tag.add(entry.getLabels().get(j).getValue());
				   }
				  }
			}
		    
			//Calcola per ogni tag il numero di volte in cui si presenta
		    Iterator<String> i = tag.iterator();
				while (i.hasNext()) {
					String str = i.next();
					if (occ.containsKey(str)) {
						occ.put(str, new Integer(occ.get(str).intValue() + 1));
					} else {
						occ.put(str, new Integer(1));
					}
				}
		}catch (IOException e){
			e.printStackTrace();
		}catch (ServiceException e){
			e.printStackTrace();
		}
		
		try{		
			
			Document doc_resp = composeResponse(nameproject, namecomponent, occ);				
			DomRepresentation dom;	
			dom = new DomRepresentation(MediaType.TEXT_XML, doc_resp );
			response.setEntity(dom);
				
		}catch (Exception e) {
			// TODO Blocco catch generato automaticamente
			e.printStackTrace();
		}			
		
	}

	
	
	
}
